// 规则引擎完整功能测试脚本
async function testRuleEngineComplete() {
  console.log('开始测试规则引擎完整功能...');
  
  try {
    const RULES_ENGINE_URL = 'http://localhost:3004';
    
    // 1. 测试获取所有规则
    console.log('\n1. 测试获取所有规则:');
    try {
      const rulesResponse = await fetch(`${RULES_ENGINE_URL}/api/rules`);
      if (rulesResponse.ok) {
        const rules = await rulesResponse.json();
        console.log('✅ 获取规则成功，规则数量:', rules.length);
        console.log('规则详情:', JSON.stringify(rules, null, 2));
      } else {
        console.log('❌ 获取规则失败，状态码:', rulesResponse.status);
      }
    } catch (error) {
      console.log('❌ 获取规则时发生网络错误:', error.message);
    }
    
    // 2. 测试获取所有变量
    console.log('\n2. 测试获取所有变量:');
    try {
      const variablesResponse = await fetch(`${RULES_ENGINE_URL}/api/variables`);
      if (variablesResponse.ok) {
        const variables = await variablesResponse.json();
        console.log('✅ 获取变量成功，变量数量:', Object.keys(variables).length);
        console.log('变量详情:', JSON.stringify(variables, null, 2));
      } else {
        console.log('❌ 获取变量失败，状态码:', variablesResponse.status);
      }
    } catch (error) {
      console.log('❌ 获取变量时发生网络错误:', error.message);
    }
    
    // 3. 测试添加新规则
    console.log('\n3. 测试添加新规则:');
    try {
      const newRule = {
        name: '温度监控规则',
        description: '当温度超过阈值时触发报警',
        condition: {
          variable: 'DB1,W0.0',
          operator: '>',
          value: 30
        },
        actions: [
          {
            type: 'log',
            message: '温度过高警告: {{DB1,W0.0}}°C'
          },
          {
            type: 'plc-write',
            variable: 'DB1,W6.0',
            value: 1
          }
        ]
      };
      
      const addRuleResponse = await fetch(`${RULES_ENGINE_URL}/api/rules`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(newRule)
      });
      
      if (addRuleResponse.ok) {
        const result = await addRuleResponse.json();
        console.log('✅ 添加规则成功:', result);
      } else {
        console.log('❌ 添加规则失败，状态码:', addRuleResponse.status);
      }
    } catch (error) {
      console.log('❌ 添加规则时发生网络错误:', error.message);
    }
    
    // 4. 测试更新变量
    console.log('\n4. 测试更新变量:');
    try {
      const variablesToUpdate = {
        'DB1,W0.0': 25.5,
        'DB1,W2.0': 60,
        'DB1,W4.0': true
      };
      
      const updateVariablesResponse = await fetch(`${RULES_ENGINE_URL}/api/variables`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          variables: variablesToUpdate
        })
      });
      
      if (updateVariablesResponse.ok) {
        const result = await updateVariablesResponse.json();
        console.log('✅ 更新变量成功:', result);
      } else {
        console.log('❌ 更新变量失败，状态码:', updateVariablesResponse.status);
      }
    } catch (error) {
      console.log('❌ 更新变量时发生网络错误:', error.message);
    }
    
    // 5. 再次获取规则和变量，验证操作结果
    console.log('\n5. 验证操作结果:');
    try {
      // 获取更新后的规则
      const rulesResponse = await fetch(`${RULES_ENGINE_URL}/api/rules`);
      if (rulesResponse.ok) {
        const rules = await rulesResponse.json();
        console.log('✅ 更新后规则数量:', rules.length);
      }
      
      // 获取更新后的变量
      const variablesResponse = await fetch(`${RULES_ENGINE_URL}/api/variables`);
      if (variablesResponse.ok) {
        const variables = await variablesResponse.json();
        console.log('✅ 更新后变量数量:', Object.keys(variables).length);
        console.log('更新后的关键变量:', {
          'DB1,W0.0': variables['DB1,W0.0'],
          'DB1,W2.0': variables['DB1,W2.0'],
          'DB1,W4.0': variables['DB1,W4.0']
        });
      }
    } catch (error) {
      console.log('❌ 验证操作结果时发生网络错误:', error.message);
    }
    
    console.log('\n✅ 规则引擎完整功能测试完成!');
  } catch (error) {
    console.error('❌ 测试过程中出现错误:', error);
  }
}

// 运行测试
testRuleEngineComplete();